package com.echat.elocation.manager;

import android.content.Context;

import com.echat.elocation.constant.UserInfoKey;
import com.echat.elocation.event.UserInfo;
import com.echat.elocation.util.SharedPreferencesUtils;

import java.util.Map;

/**
 *
 * @author lihao
 *
 * @date 2018/7/11 9:49
 */
public class UserInfoManager {

    public static UserInfo getUserInfo(Context context){
        UserInfo info = new UserInfo();
        Map<String, ?> maps = SharedPreferencesUtils.getAll(context);
        info.setHost((String) maps.get(UserInfoKey.SERVER_HOST_KEY));
        info.setPort((Integer) maps.get(UserInfoKey.SERVER_PORT_KEY));
        info.setPhoneNumber((String) maps.get(UserInfoKey.PHONE_NUMBER_KEY));
        info.setTerminalId((String) maps.get(UserInfoKey.TERMINAL_ID_KEY));
        info.setCarNumber((String) maps.get(UserInfoKey.CAR_NUMBER_KEY));
        info.setMinTime((Integer) maps.get(UserInfoKey.MIN_TIME_KEY));
        return info;
    }

    public static void setUserInfo(Context context, UserInfo info){
        SharedPreferencesUtils.put(context, UserInfoKey.SERVER_HOST_KEY, info.getHost());
        SharedPreferencesUtils.put(context, UserInfoKey.SERVER_PORT_KEY, info.getPort());
        SharedPreferencesUtils.put(context, UserInfoKey.PHONE_NUMBER_KEY, info.getPhoneNumber());
        SharedPreferencesUtils.put(context, UserInfoKey.TERMINAL_ID_KEY, info.getTerminalId());
        SharedPreferencesUtils.put(context, UserInfoKey.CAR_NUMBER_KEY, info.getCarNumber());
        SharedPreferencesUtils.put(context, UserInfoKey.MIN_TIME_KEY, info.getMinTime());
        setIsSave(context, true);
    }

    public static boolean isSave(Context context){
        return (boolean) SharedPreferencesUtils.get(context, UserInfoKey.IS_SAVE_KEY, false);
    }

    public static void setIsSave(Context context, boolean isSave){
        SharedPreferencesUtils.put(context, UserInfoKey.IS_SAVE_KEY, isSave);
    }

    public static void clearInfo(Context context){
        SharedPreferencesUtils.clear(context);
    }

}
